# -*- coding: UTF-8 -*-
# Author: Slp
# @Date : 2021-08-26

import unittest

from ruamel import yaml

from src.UItest.project_page import ProjectPage
from src.UItest.base_page import BasePage
from src.common.screenshot import takeScreenshot, createDir


class ProjectTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = BasePage.get_chrome(cls)
        cls.lp = ProjectPage(cls.driver)
        cls.f = open('../../data/project.yaml', encoding='gbk')  # 读取项目模块需要的字段值信息
        cls.f_cookie = open('../../data/cookie.yaml', 'r', encoding='gbk')  # 读取登录cookie信息
        cls.d = yaml.load(cls.f_cookie.read(), Loader=yaml.FullLoader)  # 转化为字典格式

    def test_project_search(self):
        """搜索操作校验"""
        self._testProjectName = '车位通'  # 测试报告中展示
        self._testMethodDoc = '项目管理搜索校验'  # 测试报告中展示
        self._testMethodName = 'test_project_search'  # 测试报告中展示
        self._testMethodUrl = ''  # 测试报告中展示
        yaml_list = yaml.load(self.f, Loader=yaml.FullLoader)  # 将读取的数据转化为列表
        projectName = yaml_list[0]['projectName']
        url = yaml_list[0]['url']
        self.driver.get(url)  # 打开车位通系统
        self.driver.add_cookie(self.d['cookie'][0])  # 添加cookie保持登录态
        self.lp.testSearch(url, projectName)  # 打开要测试的页面
        ac = BasePage.get_title(self)
        page_source = BasePage.get_page_source(self)
        # print(page_source)
        print(ac)
        self.assertEqual(ac, "车位通")
        takeScreenshot(self.driver, createDir("jyy"), "projectNameSearch")  # 调用截图方法截图执行后页面并保存执行结果页面

    def tearDown(self):
        BasePage.quit(self)  # 关闭打开的浏览器
        self.f.close()  # 关闭打开的文件
        self.f_cookie.close()


if __name__ == "__main__":
    unittest.main(verbosity=2)
